package com.mswork.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.mswork.common.core.domain.model.LoginUser;
import com.mswork.common.utils.SecurityUtils;
import com.mswork.mes.utils.AuthorizeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mswork.common.annotation.Log;
import com.mswork.common.core.controller.BaseController;
import com.mswork.common.core.domain.AjaxResult;
import com.mswork.common.enums.BusinessType;
import com.mswork.mes.domain.StoreOrder;
import com.mswork.mes.service.IStoreOrderService;
import com.mswork.common.utils.poi.ExcelUtil;
import com.mswork.common.core.page.TableDataInfo;

/**
 * 工单管理Controller
 *
 * @author ylq
 * @date 2025-03-05
 */
@Api("工单管理")
@RestController
@RequestMapping("/mes/order")
public class StoreOrderController extends BaseController {
    @Autowired
    private IStoreOrderService storeOrderService;

    /**
     * 查询工单管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StoreOrder storeOrder) {
        startPage();
        List<StoreOrder> list = storeOrderService.selectStoreOrderList(storeOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单管理列表
     */
    @Log(title = "工单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StoreOrder storeOrder) {
        List<StoreOrder> list = storeOrderService.selectStoreOrderList(storeOrder);
        ExcelUtil<StoreOrder> util = new ExcelUtil<StoreOrder>(StoreOrder.class);
        util.exportExcel(response, list, "工单管理数据");
    }

    /**
     * 获取工单管理详细信息
     */
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return success(storeOrderService.selectStoreOrderByOrderId(orderId));
    }

    /**
     * 新增工单管理
     */
    @ApiOperation("新增工单管理")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StoreOrder storeOrder) {
        if(!AuthorizeUtils.hasPermi()){
            LoginUser loginUser = SecurityUtils.getLoginUser();
            storeOrder.setResponsiblePerson(loginUser.getUser().getNickName());
            storeOrder.setResponsibleId(loginUser.getUserId());
            return storeOrderService.insertStoreOrder(storeOrder);
        }else {
            return AjaxResult.warn("体验用户,无法使用该功能");
        }
    }

    /**
     * 修改工单管理
     */
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StoreOrder storeOrder) {
        if(!AuthorizeUtils.hasPermi()){
            return storeOrderService.updateStoreOrder(storeOrder);
        }else {
            return AjaxResult.warn("体验用户,无法使用该功能");
        }
    }

    /**
     * 删除工单管理
     */
    @Log(title = "工单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        if(!AuthorizeUtils.hasPermi()){
            return toAjax(storeOrderService.deleteStoreOrderByOrderIds(orderIds));
        }else {
            return AjaxResult.warn("体验用户,无法使用该功能");
        }
    }

    /**
     * 修改工单状态
     */
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestBody StoreOrder storeOrder) {
        return storeOrderService.updateStatus(storeOrder);
    }

    /**
     * 获取工单管理详细信息
     */
    @PostMapping(value = "/detail")
    public AjaxResult getOrderDetail(@RequestBody StoreOrder storeOrder) {
        return success(storeOrderService.getOrderDetail(storeOrder));
    }

    /**
     * 查询待办列表
     */
    @GetMapping("/todoList")
    public TableDataInfo todoList(StoreOrder storeOrder) {
        startPage();
        List<StoreOrder> list = storeOrderService.getTodoList(storeOrder);
        return getDataTable(list);
    }
}
